<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\portal\controller;
use app\portal\model\PortalCaseClassModel;
use app\portal\service\CaseService;
use app\portal\model\PortalCasePostModel;
use think\facade\Db;

class CaseController extends HomeBaseController
{
    // protected $view_share_var = array(
    //     PRODCUT_CATEGORY_LIST => array( // 产品分类列表 ，对应控制器ProductController,  
    //         'position' => false,'type'=>3,'isSelect'=>true,'controller'=>'product'
    //     ), 
    //     NEWS_CATEGORY_LIST => array(//新闻分类列表,对应控制器NewsController
    //         'position' => false,'type'=>3,'isSelect'=>true,'controller'=>'news'
    //     ), 
    //     //案例分类列表，对应控制器CaseController
    //     // CASE_CATEGORY_LIST => array(
    //     //     'position' => true,'type'=>3,'isSelect'=>true,'controller'=>'case'
    //     // ),  
    //     PRODCUT_RECOMMEND_LIST => array( // 产品推荐列表 ，对应控制器ProductController,  
    //         'position' => false,'type'=>2,'isSelect'=>false,'controller'=>'product'
    //     ),
    //     NEWS_RECOMMEND_LIST => array( // 新闻推荐列表 ，对应控制器ProductController,  
    //         'position' => false,'type'=>2,'isSelect'=>false,'controller'=>'news'
    //     ),
    //     CASE_RECOMMEND_LIST => array( // 案例推荐列表 ，对应控制器ProductController,  
    //         'position' => false,'type'=>2,'isSelect'=>true,'controller'=>'case'
    //     ),        
    // );   
    
    public $view_share_arr = array(
            CASE_CATEGORY_LIST=> array(
                'where' => array('post.post_status' => '1','post.delete_time'=>'0'),
                'order' => 'post.recommended desc,post.list_order',
                'limit' => '9',
                'field'=>'post.id,post.more,post.post_title,post.published_time,post.post_excerpt',
                'table_pre' =>'case'
            ),
            CASE_RECOMMEND_LIST=>array(
                    'where' => array('post.post_status' => '1','post.delete_time'=>'0'),
                    'order' => 'post.recommended desc,post.list_order',
                    'limit' => '9',
                    'field'=>'post.id,post.more,post.post_title,post.published_time,post.post_excerpt',
                    'table_pre' =>'case'
                ),
        );


    public function _initialize() {
        
        parent::_initialize();
                                
    }
    
    
    public function index()
    {
        $id                  = $this->request->param('id', 0, 'intval');
        $portalCategoryModel = new PortalCaseClassModel();

        $category = $portalCategoryModel->where('id', $id)->where('status', 1)->find();
        if(empty($category)){
            $category1=db('nav_menu')->where('href','/case')->find();
            $this->assign('category1', $category1);       
            $category['id'] = 0;
            $category['name'] ="案例中心";
        }
//        var_dump($category);exit;
        $this->assign('category', $category);
                 
       $listTpl = empty($category['list_tpl']) ? 'case' : $category['list_tpl'];

        return $this->fetch('case/' . $listTpl);
    }
    
    
    
    public function caseDetail()
    {
        
         $portalCategoryModel = new PortalCaseClassModel();
        $postService         = new CaseService();

        $articleId  = $this->request->param('id', 0, 'intval');
        $categoryId = $this->request->param('cid', 0, 'intval');
        $article    = $postService->publishedArticle($articleId, $categoryId)->toArray();
       

        if (empty($article)) {
            abort(404, '产品不存在!');
        }
        $categoryId = $article['category_id'];

        $prevArticle = $postService->publishedPrevArticle($articleId, $categoryId);
        $nextArticle = $postService->publishedNextArticle($articleId, $categoryId);

        
        $category = $portalCategoryModel->where('id', $categoryId)->where('status', 1)->find();

        if (empty($category)) {
            abort(404, '文章不存在!');
        }

        $this->assign('category', $category);

            
        


        $this->assign('article', $article);       
        $this->assign('prev_article', $prevArticle);
        $this->assign('next_article', $nextArticle);
       
        
        $oneTpl = empty($category['one_tpl']) ? 'case_detail' : $category['one_tpl'];
        
        return $this->fetch('case/'.$oneTpl);
      
    }
}
